<?php
include_once('common/header.php');
if($AppUI->id <= 0 || (isset($_SESSION['product']) && (($_SESSION['product']&1<<2) == 0))) {
	$this->_redirect(HOST_BACKEND);
}
//params
$intProductId		= (int)$objRequest->getParam('pproductid',0);
$intAlbumId			= (int)$objRequest->getParam('palbumid',0);
$intManufactoryId	= (int)$objRequest->getParam('pmanufactoryid',0);
$intOriginId		= (int)$objRequest->getParam('originid',0);
$intCategoryId		= (int)$objRequest->getParam('pcategoryid',0);
$intSubId			= (int)$objRequest->getParam('psubid',0);
$strName			= $objRequest->getParam('pname','');
$strShort			= $objRequest->getParam('pshort','');
$strContent			= $objRequest->getParam('pcontent','');
$oFile				= $_FILES['pimage'];
$strTag				= $objRequest->getParam('ptag','');
$strPrice			= $objRequest->getParam('pprice','');
$strFeature			= $objRequest->getParam('ptbfeature','');
$arrErr				= null;
$strImage			= '';
//data
$strName			= Ecore_Function::stripTags($strName,-2);
$strContent			= Ecore_Function::stripTags($strContent,-1);
$strShort			= Ecore_Function::stripTags($strShort,-2);
$strTag				= Ecore_Function::stripTags($strTag,-2);
$strFeature			= Ecore_Function::stripTags($strFeature,0);
//validate
if($intCategoryId < 1) array_push($arrErr, $arrLocale['category_required']);
if($strName == '') array_push($arrErr, $arrLocale['name_required']);
elseif($strName != '' && strlen($strName) < VL_NAME_MINLENGTH) array_push($arrErr, $arrLocale['name_minlength']);
if($strShort != '' && strlen($strShort) < VL_SHORT_MINLENGTH) array_push($arrErr, $arrLocale['short_minlength']);
elseif($strShort != '' && strlen($strShort) > VL_SHORT_MAXLENGTH) array_push($arrErr, $arrLocale['short_maxlength']);
if($strContent == '') array_push($arrErr, $arrLocale['content_required']);
elseif($strContent != '' && strlen($strContent) < VL_CONTENT_MINLENGTH) array_push($arrErr, $arrLocale['content_minlength']);
if(!isset($arrErr) && isset($oFile)) {
	$arrImage	= Ecore_Upload_Product::upload($oFile);
	if($arrImage['error'] == 0) {
		$strImage	= $arrImage['path'];
	}else {
		array_push($arrErr, $arrLocale['upload']);
	}
}
//update
if(!isset($arrErr)) {
	if(isset($_SESSION['PRODUCT_POST'])) unset($_SESSION['PRODUCT_POST']);
	if(isset($_SESSION['PRODUCT_ERROR'])) unset($_SESSION['PRODUCT_ERROR']);	
	$objProduct 			= new Product();
	$intError				= $objProduct->update($AppUI->id, $intProductId, $intCategoryId, $intSubId, $intManufactoryId, 
												  $intOriginId, $strName, $strShort, $strContent, $strTag, 
												  $strImage, $strPrice, $strFeature, $intAlbumId);
	if($intError == 0) Ecore_Function::setMsg($arrMessage['product_edit']);
	else array_push($arrErr, $arrLocale['noperm_product_edit']);
}
if($intError > 0) {
	$_SESSION['PRODUCT_POST']	= $objRequest->getParams();
	$_SESSION['PRODUCT_ERROR']= $arrErr;
	$this->_redirect(HOST_BACKEND . 'product/edit?productid=' . $intProductId);
}
$this->_redirect(isset($_SESSION[SESSION_BACKURL]) ? $_SESSION[SESSION_BACKURL] : HOST_BACKEND . 'product/index');